package sfms.view;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import sfms.util.PasswordValidator;

/**
 *
 * @author hy753
 */
public class ChangePassword{
    private static String errorMessage = null;
    public static String getPassword() {
        JPasswordField oldPass = new JPasswordField(10);
        JPasswordField newPass = new JPasswordField(10);
        JPasswordField confirmPass = new JPasswordField(10);
        
        Object[] objs = new Object[] {
            new JLabel("Old Password:"),
            oldPass,
            new JLabel("New Password:"),
            newPass,
            new JLabel("Confirm Password:"),
            confirmPass
        };
        int action = JOptionPane.showConfirmDialog(null, objs, "Change Password",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); 
        
        if(action == JOptionPane.CANCEL_OPTION || action == JOptionPane.CLOSED_OPTION){
            errorMessage = null;
            return null;
        }  
        else{
            if (new String(newPass.getPassword()).equals(new String(confirmPass.getPassword()))){
                if (!PasswordValidator.validate(new String(newPass.getPassword()))) {
                    errorMessage = "Password is not secure.";
                    return null;
                }
                errorMessage = null;
                return new String(newPass.getPassword());
            }
            else{
                errorMessage = "Passwords did not match.";
                return null;
            }
        }
    }
    
    public static String getLastError() {
        return errorMessage;
    }
}
